<?php
/**
 * Created by PhpStorm.
 * User: 86158
 * Date: 2021/6/14
 * Time: 18:51
 */

namespace App\Models\activity;


use App\Models\activity\observe\ActivityObserve;
use App\Models\Model;
use App\Models\User;

class Activity extends Model
{
    protected $fillable = [
        'title',
        'user_id',
        'cate_id',
        'start_time',
        'end_time',
        'apply_end_time',
        'status',
        'intro',
        'content',
        'number'
    ];

    public static function boot()
    {
        parent::boot();
        static::observe(new ActivityObserve());
    }


    /**
     * 活动创建人信息
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function user()
    {
        return $this->hasOne(User::class,'id','user_id');
    }


    /**
     * 三会一课分类
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function cate()
    {
        return $this->hasOne(ActivityCate::class,"id","cate_id");
    }

    /**
     * 状态核验
     */
    public static function checkStatus()
    {
        $data = self::where('status',1)->select(['id','status','end_time'])->get();
        foreach ($data as $item){
            if ($item['end_time'] < date("Y-m-d H:i:s")) {
                $item->status = 2;
                $item->save();
            }
        }
    }
}